﻿[cmdletbinding(DefaultParameterSetName="none")]
param
(
    [Parameter(ParameterSetName="AllServers",Mandatory=$false)] 
    [Parameter(ParameterSetName="ByServer",Mandatory=$false)] 
    [ValidateScript({(Test-Path $_ -PathType Leaf)})] 
    [String]
    $ApplicationManifestFilePath = "D:\Tools\MESF\Main\Powershell\Deployer\ApplicationConfiguration\Test.xml",

    [Parameter(ParameterSetName="AllServers",Mandatory=$false)] 
    [Parameter(ParameterSetName="ByServer",Mandatory=$false)] 
    [ValidateScript({(Test-Path $_ -PathType Leaf)})] 
    [String]
    $ManagedActionsFilePath = "D:\Tools\MESF\Main\Powershell\Deployer\ActionsRepository\ManagedActions.xml",

    [Parameter(ParameterSetName="AllServers",Mandatory=$false)] 
    [Parameter(ParameterSetName="ByServer",Mandatory=$false)] 
    [String]
    $Environment = "DEV",

    [Parameter(ParameterSetName="AllServers",Mandatory=$false)] 
    [Parameter(ParameterSetName="ByServer",Mandatory=$false)] 
    [String]
    $ApplicationName = "mytool",

    [Parameter(ParameterSetName="AllServers",Mandatory=$false)] 
    [Parameter(ParameterSetName="ByServer",Mandatory=$false)] 
    [String]
    $Version = "1.0"

)

$verboseValue = ($VerbosePreference -eq "Inquire")
$DebugValue = ($DebugPreference -eq "Inquire")

Write-Warning "$VerbosePreference => Switch is : $verboseValue"
Write-Warning "$DebugPreference => Switch is : $DebugValue"

Import-Module Application.Management.psd1 -Force
Initialize-Environment_v2 -ApplicationManifestPath $ApplicationManifestFilePath -ManagedActionsPath $ManagedActionsFilePath


$deploymentTarget = $ApplicationManifest.DeploymentTargets | ? {($_.Environment -eq $Environment) -and ($_.ApplicationName -eq $ApplicationName) -and ($_.Version -eq $Version)}
if (($deploymentTarget -eq $null) -or ($deploymentTarget.count -gt 0)) {throw new-Object System.Exception "Missing deployment Target for Application '$ApplicationName' version '$Version' on environment '$Environment' in application manifets file '$ApplicationManifestFilePath'"}

foreach($server in $deploymentTarget.Servers.Values)
{
    if (!$server.autoDownload)
    {
        #Copy Products on targeted server
        $server.Products | Select-Object $Global:DscInstallerWrapper | Foreach-Object {
            if ($_.PackageType -ne "none")
            {

                #Test Path for source copy
                $parentPath = [System.IO.Path]::GetDirectoryName($_.ProductDownload.NetworkLocalPath)
                if (-not (Test-Path -Path $parentPath -PathType Container))
                {
                    New-Item -Path $parentPath -ItemType Directory | Out-Null
                }

                Write-Host "Copy $($_.ProductDownload.RemotePath) on $($_.ProductDownload.NetworkLocalPath) in progres..." -ForegroundColor Green
                Copy-Item -Path $_.ProductDownload.RemotePath -Destination $_.ProductDownload.NetworkLocalPath
                Write-Host "Package $($_.ProductDownload.RemotePath) has been copied on  $($_.ProductDownload.NetworkLocalPath)"  -ForegroundColor Green

            }
        }

        #Copy deploymentTarget on Targeted server
        $server.ConfigurationRepositories | Select-Object $Global:DscConfigurationRepositoryObject | ForEach-Object {
            if (Test-Path -Path $_.ConfigurationRepositoryPath)
            {
                #Test Path for source copy
                $parentPath = [System.IO.Path]::GetDirectoryName($_.LocalConfigurationNetworkPath)
                if (-not (Test-Path -Path $parentPath -PathType Container))
                {
                    New-Item -Path $parentPath -ItemType Directory | Out-Null
                }

                Write-Host "Copy $($_.ConfigurationRepositoryPath) on $parentPath in progres..." -ForegroundColor Green
                Copy-Item -Path $_.ConfigurationRepositoryPath -Destination $parentPath -Force -Recurse
                Write-Host "Package $($_.ConfigurationRepositoryPath) has been copied on  $parentPath"  -ForegroundColor Green
                
            }
        }

    }
}


$dscConfigurationOutputPath = Join-Path -Path "D:\Tools\MESF\Main\Powershell\Deployer\DeploymentManifest" -ChildPath $deploymentTarget.environment
$dscConfigurationOutputPath = Join-Path -Path $dscConfigurationOutputPath -ChildPath $deploymentTarget.ApplicationName
$dscConfigurationOutputPath = Join-Path -Path $dscConfigurationOutputPath -ChildPath $deploymentTarget.Version
$dscConfigurationOutputPath = Join-Path -Path $dscConfigurationOutputPath -ChildPath "Servers"

Start-DscConfiguration -Wait -Force -Verbose -Path $dscConfigurationOutputPath


foreach ($server in $deploymentTarget.Servers.Values)
{
    #Restart-Computer -ComputerName $server.HostName  -Force -Timeout 50 -Wait 
}


Write-Output $deploymentTarget
